package com.rfid.emergencywarehouse.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.rfid.base.util.AnimatorHelper;
import com.rfid.base.util.BaseUtil;
import com.rfid.base.util.PreferenceUtil;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.activity.HomeAcitvity;
import com.rfid.emergencywarehouse.event.LoginEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginInputFragment extends Fragment {
    @BindView(R.id.user)
    EditText mUser;
    @BindView(R.id.password)
    EditText mPwd;
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
//        mUser.setText("zhongpeng");
//        mPwd.setText("Abc123--");
        String name = PreferenceUtil.getString("name","");
        String pwd = PreferenceUtil.getString("password","");
        mUser.setText(name);
        mPwd.setText(pwd);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_login_input,container,false);
    }

    @OnClick(R.id.go_face)
    public void goFace(View view) {
        NavHostFragment.findNavController(this)
                .navigate(R.id.action_loginInputFragment_to_loginFaceFragment,null);
    }

    @OnClick(R.id.login)
    public void login(View v){
        String user = mUser.getEditableText().toString();
        String pwd = mPwd.getEditableText().toString();

        if (TextUtils.isEmpty(user)) {
            AnimatorHelper.shake(getContext(),mUser);
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            AnimatorHelper.shake(getContext(),mPwd);
            return;
        }
        EventBus.getDefault().post(new LoginEvent(user,pwd));
        PreferenceUtil.commitString("name",user);
        PreferenceUtil.commitString("password",pwd);
    }
}
